package com.taotao.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ContentService;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("${TB_CONTENT_KEY}")
	private String TB_CONTENT_KEY;
	
	@Override
	public TaotaoResult getContentListByCid(Long contentCid) {
		if(contentCid==null) {
			return TaotaoResult.build(400, "错误的参数");
		}
		String key=""+contentCid;
		try {
			String data = jedisClient.hget("TB_CONTENT_KEY", key);
			if(!StringUtils.isBlank(data)) {
				return TaotaoResult.ok(data);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		TbContentExample example=new TbContentExample();
		example.createCriteria().andCategoryIdEqualTo(contentCid);
		List<TbContent> list = contentMapper.selectByExample(example);
		String data=JsonUtils.objectToJson(list);
		
		
		try {
			jedisClient.hset("TB_CONTENT_KEY", key, data);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return TaotaoResult.ok(data);
	}

}
